# 多智能体协作实现浏览器自动化，使用PlayWright Browser Toolkit
# 在https://python.langchain.com/docs/integrations/tools/查看详情
# pip install playwright lxml langchain_community beautifulsoup4 reportlab
# 此外还需要安装playwright浏览器，conda中执行: playwright install
def main():
    import os
    from dotenv import load_dotenv
    from langchain_community.tools.playwright.utils import create_sync_playwright_browser
    from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
    from langchain import hub
    from langchain.chat_models import init_chat_model
    from langchain.agents import AgentExecutor, create_tool_calling_agent, tool, create_openai_tools_agent
    load_dotenv(override=True)
    DeepSeek_API_KEY = os.getenv("DEEPSEEK_API_KEY")

    sync_browser = create_sync_playwright_browser()
    toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=sync_browser)
    tools = toolkit.get_tools()
    prompt = hub.pull("hwchase17/openai-tools-agent") # 从langchain hub上拉取提示词模板
    model = init_chat_model(model="deepseek-chat", model_provider="deepseek")
    agent = create_openai_tools_agent(model, tools, prompt)
    agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
    command = {
        "input": "访问这个网站 https://blog.csdn.net/weixin_43807749/article/details/149905815，并帮我总结一下主要内容"}
    response = agent_executor.invoke(command)
    print(response)

# 按装订区域中的绿色按钮以运行脚本。
if __name__ == '__main__':
    main()